package com.sc.controller.ahao;

import cn.hutool.core.bean.BeanUtil;
import com.sc.domain.PayItem;
import com.sc.dto.PayItemCreateDTO;
import com.sc.dto.PayItemPageQueryDTO;
import com.sc.dto.PayItemUpdateDTO;
import com.sc.result.PageResult;
import com.sc.result.Result;
import com.sc.service.PayItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pay/item")
@Api(tags = "收费项目接口")
@Slf4j
@CrossOrigin
public class PayItemController {

    @Autowired
    PayItemService payItemService;

    @ApiOperation("收费项目添加")
    @PostMapping("/createItem")
    public Result createItem(@RequestBody PayItemCreateDTO requestParam) {
        PayItem payItem = new PayItem();
        BeanUtil.copyProperties(requestParam, payItem); // 拷贝属性
        payItemService.save(payItem);
        return Result.success("添加成功", null);
    }

    @PostMapping("/pageQuery")
    public Result<PageResult<PayItem>> pageQueryItem(@RequestBody PayItemPageQueryDTO requestParam) {
        PageResult<PayItem> pageResult = payItemService.pageQueryItem(requestParam);
        return Result.success(pageResult);
    }

    @PutMapping("/updateItem")
    public Result updateItem(@RequestBody PayItemUpdateDTO requestParam) {
        PayItem payItem = new PayItem();
        BeanUtil.copyProperties(requestParam, payItem); // 拷贝属性
        payItemService.updateById(payItem);
        return Result.success("修改成功", null);
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteItem(@PathVariable("id") Long id) {
        return Result.success("删除成功", payItemService.removeById(id));
    }

    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Long> ids) {
        return Result.success("删除成功", payItemService.removeByIds(ids));
    }

    @GetMapping("/get/{id}")
    public Result<PayItem> queryItemById(@PathVariable("id") Long id) {
        return Result.success(payItemService.getById(id));
    }

    @GetMapping("/getByName")
    public Result<PayItem> getByName(String name) {
        return Result.success(payItemService.getByName(name));
    }

    @GetMapping("/getAll")
    public Result<List<PayItem>> getAll() {
        return Result.success(payItemService.list());
    }
}
